import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { registerMicroApps, loadMicroApp, start } from 'qiankun'


let vm = null
/**
 * 渲染函数
 * appContent 子应用html内容
 * loading 子应用加载效果，可选
 */
function render ({ appContent, loading } = {}) {
    if (!vm) {
        vm = new Vue({
            router,
            store,
            data () {
                return {
                    content: appContent,
                    loading
                }
            },
            render (h) {
                return h(App, {
                    props: {
                        content: this.content,
                        loading: this.loading
                    }
                })
            }
        }).$mount('#root')
    } else {
        vm.content = appContent
        vm.loading = loading
    }
}
function initApp () {
    render({ appContent: '', loading: true })
}

/**
 * 路由监听
 * @param {*} routerPrefix 前缀
 */
function genActiveRule (routerPrefix) {
    return location => location.pathname.startsWith(routerPrefix)
}


registerMicroApps([
    //微应用--child-web1--index.html
	{
		name: 'child-web1-index',
		entry: '//localhost:8081/child-web1/index.html',
		//render,
        container: '#qiankun-root',
		activeRule: genActiveRule('/child-web1/index.html'),
	},
    //微应用--child-web1--index.html
    {
        name: 'child-web1-book',
        entry: '//localhost:8081/child-web1/book.html',
        //render,
        container: '#qiankun-root',
        activeRule: genActiveRule('/child-web1/book.html'),
    },
    //微应用--child-web2--index.html
    {
        name: 'child-web2-index',
        entry: '//localhost:8082/child-web2/index.html',
        //render,
        container: '#qiankun-root',
        activeRule: genActiveRule('/child-web2/index.html'),
    },
    //微应用--child-web2--user.html
    {
        name: 'child-web2-user',
        entry: '//localhost:8082/child-web2/user.html',
        //render,
        container: '#qiankun-root',
        activeRule: genActiveRule('/child-web2/user.html'),
    },
]);

initApp();
//start();
start({
    jsSandbox: true,
    // sandbox: {
    //     strictStyleIsolation: true
    // }
});
//loadMicroApp({}, { sandbox: { strictStyleIsolation: true } });
